<?php
/**
 * Created by PhpStorm.
 * User: lullabies
 * Date: 2019/5/16
 * Time: 10:18
 */

namespace app\api\model;


use think\Model;
use think\model\concern\SoftDelete;

class User extends Model
{
    //设置隐藏字段
    protected $hidden = ['create_time','update_time','status'];
    /**
     * 设置与考试的多对多的关联
     */
    public function userExam(){
        return $this->belongsToMany('exam','TotalGrade','exam_id','user_id');
    }
    /**
     * 根据主键id获取用户相关信息
     */
    public static function getUserInfoById($id){
        $result = self::get($id);
        return $result;
    }
    /**
     * 根据用户名查找用户
     */
    public static function findUserByName($username){
        $result = self::where('username','=',$username)->find();
        return $result;
    }
    /**
     * 根据手机号查找用户
     */
    public static function findUserByPhone($phone){
        $result = self::where(['phone'=>$phone,'status'=>1]) -> find();
        return $result;
    }
    /**
     * 用户注册
     */
    public static function registerByPhone($data){
        $register = self::create($data);
        return $register;
    }
    /**
     * 用户认证
     */
    public static function authentication($data,$user_id){
        $result = self::where('id',$user_id)
            ->update($data);
        return $result;
    }
    /**
     * 根据用户id获取联考名称，总分数
     */
    public static function getPersonExamInfoById($user_id){
        $result = self::field('id,username,img,phone')
            ->with('userExam')->where('status',1)->find($user_id);
        return $result;
    }

}